package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import net.sf.json.JSONObject;

/**
 * 拼车常用地址
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobNomalPlace extends AbstractFormPlugin {

	private String[] buttons = new String[] { "home", "company" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		if (StringUtils.equals("home", key)) {// 家地址
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_searchplace");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			CloseCallBack closeCallBack = new CloseCallBack(this, "homeset");
			showParameter.setCloseCallBack(closeCallBack);
			view.showForm(showParameter);
		} else if (StringUtils.equals("company", key)) {// 公司地址
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_searchplace");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			CloseCallBack closeCallBack = new CloseCallBack(this, "companyset");
			showParameter.setCloseCallBack(closeCallBack);
			view.showForm(showParameter);
		}
	}

	@Override
	public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
		super.closedCallBack(closedCallBackEvent);
		String actionId = closedCallBackEvent.getActionId();
		Map<String, Object> re = (Map<String, Object>) closedCallBackEvent.getReturnData();
		String place = re.get("place").toString();
		long userId = Long.parseLong(RequestContext.get().getUserId());
		long orgId = RequestContext.get().getOrgId();
		QFilter reportfilter = new QFilter("creator", QCP.equals, userId);
		reportfilter.and(new QFilter("orgfield", QCP.equals, orgId));
		DynamicObject report = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincar_reportbill",
				reportfilter.toArray());
		if (StringUtils.equals("homeset", actionId) && !StringUtils.equals("", place)) {

			JSONObject local = JSONObject.fromObject(re.get("local"));
			// 更新乘客记录字段-家
			if (null == report) {// 无乘客数据
				report = BusinessDataServiceHelper.newDynamicObject("kdec_pincar_reportbill");// 表单标识
				// 设置单据属性
				report.set("billno", System.currentTimeMillis());
				report.set("billstatus", "B");
				report.set("creator", userId);
				report.set("orgfield", RequestContext.get().getOrgId());
				report.set("home", place);
				report.set("homelocal", local);
			} else if (report != null) {// 有数据
				// 更新
				report.set("home", place);
				report.set("homelocal", local);
			}
			SaveServiceHelper.save(new DynamicObject[] { report });
			Label label = this.getControl("homeplace");
			label.setText(place);

		} else if (StringUtils.equals("companyset", actionId) && !StringUtils.equals("", place)) {

			JSONObject local = JSONObject.fromObject(re.get("local"));
			// 更新乘客记录字段-家
			if (null == report) {// 无乘客数据
				report = BusinessDataServiceHelper.newDynamicObject("kdec_pincar_reportbill");// 表单标识
				// 设置单据属性
				report.set("billno", System.currentTimeMillis());
				report.set("billstatus", "B");
				report.set("creator", userId);
				report.set("orgfield", RequestContext.get().getOrgId());
				report.set("company", place);
				report.set("companylocal", local);
			} else if (report != null) {// 有数据
				// 更新
				report.set("company", place);
				report.set("companylocal", local);
			}
			SaveServiceHelper.save(new DynamicObject[] { report });
			Label label = this.getControl("companyplace");
			label.setText(place);

		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		long userId = Long.parseLong(RequestContext.get().getUserId());
		long orgId = RequestContext.get().getOrgId();
		QFilter reportfilter = new QFilter("creator", QCP.equals, userId);
		reportfilter.and(new QFilter("orgfield", QCP.equals, orgId));
		DynamicObject report = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincar_reportbill",
				reportfilter.toArray());
		Label homeplace = this.getControl("homeplace");
		Label companyplace = this.getControl("companyplace");
		String home = report.getString("home");
		String company = report.getString("company");
		if (StringUtils.equals("", home)) {
			this.getView().setVisible(false, "homeplace");
		} else {
			homeplace.setText(home);
		}
		if (StringUtils.equals("", company)) {
			this.getView().setVisible(false, "companyplace");
		} else {
			companyplace.setText(company);
		}
	}

}
